home *** CD-ROM | disk | FTP | other *** search
/ BMUG PD-ROM A / PD-ROM A.iso / Utility / Random / DarkSide 2.5 / FaderShell / Blackout.c next >
Encoding:
Text File  |  1991-08-08  |  19.4 KB  |  762 lines  |  [TEXT/MPS ]

  1. /*
  2.  
  3.     Blackout.c - A simple screen fader that runs as a MultiFinder
  4.     application.  Is typically launched by Lunarmobiscuit's
  5.     Darkness MultiFinder screen saver program.
  6.     
  7.     copyright © 1989, 1990, 1991 by Tom Dowdy
  8.     All rights reserved
  9.     
  10.     Modifications
  11.         Aug    5 1989        TED        basic fade working as standalone code
  12.         Nov 30 1989        TED        file converted to a standalone application
  13.         Feb 25 1991        CJR        Added support for multiwindow faders
  14.         
  15.     Assumptions:
  16.         MultiFinder is present.
  17.         
  18.     Slightly questionables:
  19.         Uses lowMem mBarHeight for setting menu bar height to 0.
  20.         Modifies a window's visRgn to include the menu bar.
  21.         
  22.     
  23. */
  24.  
  25. // Standard MacIncludes - you might wish to use a dump file for this
  26. #include <Limits.h>
  27. #include <Types.h>
  28. #include <Resources.h>
  29. #include <QuickDraw.h>
  30. #include <Fonts.h>
  31. #include <Events.h>
  32. #include <Windows.h>
  33. #include <Menus.h>
  34. #include <TextEdit.h>
  35. #include <Dialogs.h>
  36. #include <Desk.h>
  37. #include <ToolUtils.h>
  38. #include <Memory.h>
  39. #include <SegLoad.h>
  40. #include <Devices.h>
  41. #include <Files.h>
  42. #include <OSUtils.h>
  43. #include <OSEvents.h>
  44. #include <Traps.h>    
  45. #include <SysEqu.h>
  46. #include <Script.h>
  47. #include <Picker.h>
  48. #include <FixMath.h>
  49. #include <Packages.h>
  50. #include <Math.h>
  51. #include <Palette.h>
  52. #include <QDOffscreen.h>
  53. #include <Devices.h>
  54. #include <DeskBus.h>
  55. #include <StdLib.h>
  56. #include <StdArg.h>
  57. #include <Strings.h>
  58. #include <StdIO.h>
  59. #include <DiskInit.h>
  60.  
  61. // Defines to let this include work okay
  62. #define INMAINBLACKOUT
  63. typedef Handle BlackoutHandle;
  64.  
  65. #include "Blackout.h"
  66.  
  67.  
  68. /* ------------------------------------------------------------------------------------    */
  69. /*    INTERNAL ROUTINES - Shouldn't need to be changed by Blackout authors.                */
  70. /* ------------------------------------------------------------------------------------    */
  71. #define mBarHeight    (short *)0x0BAA        // Low mem global for menu bar
  72. #define    kOSEvent                app4Evt    // event used by MultiFinder
  73. #define    kSuspendResumeMessage    1        // high byte of suspend/resume event message
  74. #define    kResumeMask                1        // bit of message field for resume vs. suspend
  75.  
  76. #define TopLeft(aRect)    (* (Point *) &(aRect).top)
  77. #define BotRight(aRect)    (* (Point *) &(aRect).bottom)
  78.  
  79.  
  80. /* This routine is part of the MPW runtime library. This external
  81.    reference to it is done so that we can unload its segment, %A5Init. */
  82. extern void _DataInit();
  83.  
  84. SysEnvRec    theEnvirons;
  85. Boolean        gHas32BitQD;
  86.  
  87. /* ------------------------------------------------------------------------------------    */
  88. #pragma segment Main
  89.  
  90. void PlaceRectOnScreen(
  91.     short width,        // width of rect, can be 0
  92.     short height,        // height of rect, can be 0
  93.     Rect * placedRect,    // Placed rect is returned here
  94.     Rect * margins,        // margins around screen, can be nil
  95.     Rect * screenBound)    // placed screen bounds returned here, can be nil
  96. {
  97.     Rect        screenRect;
  98.     short        deviceCount;
  99.     GDHandle    theDevice;
  100.     
  101.     if (theEnvirons.hasColorQD)
  102.         {
  103.         // First, count the GDevices
  104.         deviceCount = 0;
  105.         theDevice = GetDeviceList();
  106.         while (theDevice != nil)
  107.             {
  108.             if ( (TestDeviceAttribute(theDevice, screenDevice)) &&
  109.                 (TestDeviceAttribute(theDevice, screenActive)) )
  110.                 deviceCount++;
  111.             theDevice = GetNextDevice(theDevice);
  112.             }
  113.             
  114.         // Then pick one at random
  115.         deviceCount = Rnd(deviceCount)+1;
  116.         
  117.         // Fetch that GDevice
  118.         theDevice = GetDeviceList();
  119.         while (deviceCount != 0)
  120.             {
  121.             if ( (TestDeviceAttribute(theDevice, screenDevice)) &&
  122.                 (TestDeviceAttribute(theDevice, screenActive)) )
  123.                 {
  124.                 deviceCount--;
  125.                 if (deviceCount == 0)
  126.                     screenRect = (**theDevice).gdRect;
  127.                 }
  128.             theDevice = GetNextDevice(theDevice);
  129.             }
  130.         
  131.         }
  132.     else
  133.         screenRect = (**GetGrayRgn()).rgnBBox;
  134.         
  135.     // Localize the points
  136.     GlobalToLocal(&TopLeft(screenRect));
  137.     GlobalToLocal(&BotRight(screenRect));
  138.     
  139.     if (margins != nil)
  140.         {
  141.         screenRect.top += margins->top;
  142.         screenRect.left += margins->left;
  143.         screenRect.bottom -= margins->bottom;
  144.         screenRect.right -= margins->right;
  145.         }
  146.     if (screenBound != nil)
  147.         *screenBound = screenRect;
  148.         
  149.     screenRect.right -= width;
  150.     screenRect.bottom -= height;
  151.     
  152.     placedRect->top = screenRect.top + Rnd(screenRect.bottom - screenRect.top);
  153.     placedRect->left = screenRect.left + Rnd(screenRect.right - screenRect.left);
  154.     placedRect->bottom = placedRect->top + height;
  155.     placedRect->right = placedRect->left + width;
  156.     
  157. } // PlaceRectOnScreen
  158.  
  159. /* ------------------------------------------------------------------------------------    */
  160. #pragma segment Main
  161.  
  162. short    Rnd(long max)
  163. {
  164.     long    value;
  165.     
  166.     value = max * Random();
  167.     if (value < 0)
  168.         value = -value;
  169.     value /= 32767;
  170.     if (value == max)
  171.         value--;
  172.     return(value);
  173.     
  174. } // Rnd
  175.  
  176. /* ------------------------------------------------------------------------------------    */
  177. #pragma segment Main
  178.  
  179. void AddMenuBarIntoVisRgn(WindowPtr theWindow, short oldMenuBarHeight)
  180. {
  181.     RgnHandle    mBarRgn;
  182.     Rect        mBarRect;
  183.     GrafPtr        oldPort;
  184.         
  185.     /*    At this point, our new window doesn't include the menu bar,
  186.         so make a region out of the menu bar, and add it onto
  187.         the window's visRgn */
  188.     GetPort( &oldPort) ;
  189.     SetPort( (GrafPtr)theWindow );
  190.     
  191.     mBarRgn = NewRgn();
  192.     if (mBarRgn != nil)
  193.         {
  194.         /* Calculate the rect of the menu bar */
  195.         mBarRect = qd.screenBits.bounds;
  196.         mBarRect.bottom = mBarRect.top + oldMenuBarHeight;
  197.         
  198.         /* ScreenBit is in global, vis regions are local */
  199.         GlobalToLocal(&TopLeft(mBarRect));
  200.         GlobalToLocal(&BotRight(mBarRect));
  201.         
  202.         /* Make a region out of the menu bar */
  203.         RectRgn(mBarRgn, &mBarRect);
  204.         
  205.         /* Tack this region onto the visRgn */
  206.         UnionRgn(theWindow->visRgn, mBarRgn, theWindow->visRgn);
  207.                 
  208.         /* Get rid of our temp region */
  209.         DisposeRgn(mBarRgn);
  210.         } // mBarRgn != nil
  211.         
  212.     SetPort( oldPort);
  213.         
  214. } // AddMenuBarIntoVisRgn
  215.  
  216. /* ------------------------------------------------------------------------------------    */
  217. /*    OFFSCREEN PIXMAP UTILTIES                                                            */
  218. /* ------------------------------------------------------------------------------------    */
  219. void    MyDisposeGWorld (GWorldPtr offscreenGWorld);
  220.  
  221. #pragma segment Initialize
  222.  
  223. void MyNewGWorld(GWorldPtr *offscreenGWorld,short PixelDepth,Rect *boundsRect,
  224.     CTabHandle cTable,GDHandle aGDevice,GWorldFlags flags)
  225. {
  226.     long        offRowBytes, sizeOfOff;
  227.     Ptr            myBits;
  228.     GrafPtr        curPort;
  229.     GDHandle    oldDevice;
  230.     CTabHandle    ourCMHandle;
  231.     short        i;
  232.     
  233.     GetPort(&curPort);
  234.             
  235.     gHas32BitQD = false;        
  236.     if (theEnvirons.hasColorQD)
  237.         {
  238.         if (NGetTrapAddress (0xAB03, ToolTrap) != NGetTrapAddress (0xA89F, ToolTrap))
  239.             gHas32BitQD = true;
  240.         }
  241.         
  242.     if (gHas32BitQD)
  243.         {
  244.         if (NewGWorld (offscreenGWorld, PixelDepth, boundsRect, cTable, aGDevice, flags) != noErr)
  245.             *offscreenGWorld = nil;
  246.         return;
  247.         }
  248.     else
  249.         {
  250.         *offscreenGWorld = (GWorldPtr) NewPtr(sizeof(CGrafPort));
  251.         if (*offscreenGWorld == nil)
  252.             return;
  253.  
  254.         if (theEnvirons.hasColorQD)
  255.             {
  256.             oldDevice = GetGDevice();
  257.             if (aGDevice != nil)
  258.                 SetGDevice(aGDevice);
  259.             OpenCPort((CGrafPtr) *offscreenGWorld);
  260.             }
  261.         else
  262.             {
  263.             OpenPort((GrafPtr) *offscreenGWorld);
  264.             }
  265.             
  266.         offRowBytes = ((((PixelDepth * (RectWidth(boundsRect))) + 15)) / 16) * 2;
  267.         sizeOfOff = ((long) RectHeight(boundsRect)) * offRowBytes;
  268.         myBits = NewPtr(sizeOfOff);
  269.         
  270.         if (myBits == nil)
  271.             {
  272.             MyDisposeGWorld(*offscreenGWorld);
  273.             *offscreenGWorld = nil;
  274.             goto cleanUp;
  275.             }
  276.             
  277.         if (theEnvirons.hasColorQD)
  278.             {
  279.             (** ((CGrafPtr) *offscreenGWorld)->portPixMap).baseAddr = myBits;
  280.             (** ((CGrafPtr) *offscreenGWorld)->portPixMap).rowBytes = offRowBytes + 0x8000;
  281.             (** ((CGrafPtr) *offscreenGWorld)->portPixMap).bounds = *boundsRect;
  282.             (** ((CGrafPtr) *offscreenGWorld)->portPixMap).pmTable = nil;
  283.             
  284.             // and clone that color table!
  285.             ourCMHandle = (**(**aGDevice).gdPMap).pmTable;
  286.             if (HandToHand( &(Handle) ourCMHandle) == noErr)
  287.                 {
  288.                 for (i = 0; i <= (**ourCMHandle).ctSize; ++i)
  289.                     (**ourCMHandle).ctTable[i].value = i;
  290.                 (**ourCMHandle).ctFlags &= 0x7fff;
  291.                 offRowBytes = GetCTSeed();
  292.                 (**ourCMHandle).ctSeed = offRowBytes;
  293.                 (** ((CGrafPtr) *offscreenGWorld)->portPixMap).pmTable = ourCMHandle;
  294.                 }
  295.             else
  296.                 {
  297.                 MyDisposeGWorld(*offscreenGWorld);
  298.                 *offscreenGWorld = nil;
  299.                 goto cleanUp;
  300.                 }
  301.                 
  302.             }
  303.         else
  304.             {
  305.             ((GrafPtr) *offscreenGWorld)->portBits.baseAddr = myBits;
  306.             ((GrafPtr) *offscreenGWorld)->portBits.rowBytes = offRowBytes;
  307.             ((GrafPtr) *offscreenGWorld)->portBits.bounds = *boundsRect;
  308.             }
  309.         }
  310.         
  311. cleanUp:        
  312.     SetPort(curPort);
  313.     if (theEnvirons.hasColorQD)
  314.         SetGDevice(oldDevice);
  315.         
  316.     return;
  317. }
  318.  
  319. /* ------------------------------------------------------------------------------------    */
  320. #pragma segment Terminate
  321.  
  322. void    MyDisposeGWorld (GWorldPtr offscreenGWorld)
  323. {
  324.     if (offscreenGWorld != nil)
  325.         if (gHas32BitQD)
  326.             {
  327.             DisposeGWorld(offscreenGWorld);
  328.             }
  329.         else
  330.             {
  331.             if (theEnvirons.hasColorQD)
  332.                 {
  333.                 CloseCPort((CGrafPtr) offscreenGWorld);
  334.                 if ( (** ((CGrafPtr) offscreenGWorld)->portPixMap).pmTable != nil) 
  335.                     DisposHandle((Handle) (** ((CGrafPtr) offscreenGWorld)->portPixMap).pmTable);
  336.                 }
  337.             else
  338.                 ClosePort((GrafPtr) offscreenGWorld);
  339.                 
  340.             if ((Ptr) ((GrafPtr) offscreenGWorld)->portBits.baseAddr != nil)
  341.                 DisposPtr((Ptr) ((GrafPtr) offscreenGWorld)->portBits.baseAddr);
  342.             DisposPtr((Ptr) offscreenGWorld);
  343.             }
  344. }
  345.  
  346. /* ------------------------------------------------------------------------------------    */
  347. #pragma segment Main
  348.  
  349. void MySetGWorld(GWorldPtr offscreenGWorld)
  350. {
  351.     if (offscreenGWorld != nil)
  352.         {
  353.         if (gHas32BitQD)
  354.             SetGWorld (offscreenGWorld, nil);
  355.         else
  356.             SetPort((GrafPtr) offscreenGWorld);
  357.         }
  358. }
  359. /* ------------------------------------------------------------------------------------    */
  360. #pragma segment Main
  361.  
  362. void MyLockPixels(GWorldPtr offscreenGWorld)
  363. {
  364.     if (offscreenGWorld != nil)
  365.         if (gHas32BitQD)
  366.             LockPixels (offscreenGWorld->portPixMap);
  367. }
  368. /* ------------------------------------------------------------------------------------    */
  369. #pragma segment Main
  370.  
  371. void MyUnlockPixels(GWorldPtr offscreenGWorld)
  372. {
  373.     if (offscreenGWorld != nil)
  374.         if (gHas32BitQD)
  375.             UnlockPixels (offscreenGWorld->portPixMap);
  376. }
  377.  
  378. /* ------------------------------------------------------------------------------------    */
  379. #pragma segment Main
  380.  
  381. Ptr MyGetPixBaseAddr(PixMapHandle portPixMap)
  382. {
  383.     if (portPixMap != nil)
  384.         {
  385.         if (gHas32BitQD)
  386.             return (GetPixBaseAddr(portPixMap));
  387.         else
  388.             return((**portPixMap).baseAddr);
  389.         }
  390.     else
  391.         return(nil);
  392. }
  393.  
  394. /* ------------------------------------------------------------------------------------    */
  395. /* ------------------------------------------------------------------------------------    */
  396. #pragma segment Main
  397.  
  398. void        AppleShareDialogs(Boolean turnOn)
  399. {
  400. #define    AFPSetAlertState    247
  401. #define    DontShowAlerts        1
  402. #define    DoShowAlerts        0
  403. #define    AFPTransRefNum        -42
  404.  
  405.     short    csParam[11];                /*operation-defined parameters*/
  406.     
  407.     if (turnOn)
  408.         csParam[1] = DoShowAlerts;
  409.     else
  410.         csParam[0] = DontShowAlerts;
  411.  
  412.     (void) Control(AFPTransRefNum,AFPSetAlertState,(Ptr)csParam);
  413.         
  414. } // AppleShareDialogs
  415.  
  416. /* ------------------------------------------------------------------------------------    */
  417. #pragma segment Initialize
  418.  
  419. WindowPtr    CreateBigWindow(short oldMenuBarHeight)
  420. {
  421.     Rect        theRect;
  422.     RgnHandle    theGrayRgn;
  423.     WindowPtr    theWindow;
  424.     
  425.     /* Get the full size of the desktop */
  426.     theGrayRgn = GetGrayRgn();
  427.     
  428.     /* Get the bounding box for the gray region */
  429.     theRect = (**theGrayRgn).rgnBBox;
  430.     
  431.     /* Add on the screenbits.bounds, which will include the menu bar */
  432.     UnionRect(&theRect, &qd.screenBits.bounds, &theRect);
  433.             
  434.     /* Make a big window of this size */
  435.     if (theEnvirons.hasColorQD)
  436.         theWindow = NewCWindow(nil, &theRect, "\p", true,
  437.             plainDBox, (WindowPtr) -1,  false, 0);
  438.     else
  439.         theWindow = NewWindow(nil, &theRect, "\p", true,
  440.             plainDBox, (WindowPtr) -1,  false, 0);
  441.         
  442.     /* If got the window okay, time to modify the visRgn */
  443.     if (theWindow != nil)
  444.         {
  445.         /* Set port to it */
  446.         SetPort(theWindow);
  447.             
  448.         AddMenuBarIntoVisRgn(theWindow, oldMenuBarHeight);
  449.         } // theWindow != nil
  450.         
  451.     return(theWindow);
  452.  
  453. } // CreateBigWindow
  454.  
  455. /* ------------------------------------------------------------------------------------    */
  456. /* PRIVATE GLOBAL AREA                                                                    */
  457. /* ------------------------------------------------------------------------------------    */
  458.     // Misc variables
  459.     short        gOldMenuBarHeight;        // For restoring the old menu bar
  460.     short        gCount;                    // Misc loop variable
  461.     
  462.     // State information for the Blackout effect that is in use
  463.     WindowPtr    gBlackoutWindow;
  464.     Handle        gTheBlackout;
  465.     long        gMinSleepTime;
  466.     long        gMaxSleepTime;
  467.     long        gSleepTime;
  468.     Boolean        gHaveBlackout;
  469.     
  470.     // Variables to keep track of events and when to terminate the application
  471.     Point        gOldMouse;
  472.     short        gOldModifiers;
  473.     long        gOldTime;
  474.     Boolean        gContinueRunning;
  475.     EventRecord    gTheEvent;
  476. /* ------------------------------------------------------------------------------------    */
  477. #pragma segment Initialize
  478.  
  479. void Initialize()
  480. {
  481.     unsigned long    aSeed;
  482.  
  483.     UnloadSeg((Ptr) _DataInit);        /* note that _DataInit must not be in Main! */
  484.  
  485.     MaxApplZone();                    /* expand the heap so code segments load at the top */    
  486.     
  487.     /* Initialize the toolbox */
  488.     InitGraf((Ptr) &qd.thePort);
  489.     InitFonts();
  490.     InitWindows();
  491.     InitMenus();
  492.     TEInit();
  493.     InitDialogs(nil);
  494.     InitCursor();
  495.     
  496.     // Seed the random number generator
  497.     GetDateTime(&aSeed);
  498.     qd.randSeed = aSeed;
  499.  
  500.     /*    We need a menu or MultiFinder will drop into Macsbug.  In addition, it
  501.         should be a File:Quit menu so that MultiFinder can shut us down via
  502.         a puppet string. */
  503.     InsertMenu(GetMenu(1), 0);
  504.     DrawMenuBar();
  505.     
  506.     /* Get our application to the front */
  507.     for (gCount = 1; gCount <= 4; ++gCount)
  508.         EventAvail(everyEvent, &gTheEvent);
  509.         
  510.     /* Turn off AppleShare dialogs - not currently supported in the AFP driver */
  511.     //AppleShareDialogs(false);
  512.     
  513.     /* Save away the old menu bar height and set it to zero */
  514.     gOldMenuBarHeight = GetMBarHeight();
  515.     *mBarHeight = 0;
  516.     
  517.     /* Find out if we have color QuickDraw */
  518.     SysEnvirons(1, &theEnvirons);
  519.  
  520.     gMinSleepTime = 0;
  521.     gMaxSleepTime = 1;
  522.     gSleepTime = 1;
  523.     gTheBlackout = PreflightBlackout(&gMinSleepTime, &gMaxSleepTime);
  524.  
  525.     gBlackoutWindow = CreateBigWindow(gOldMenuBarHeight);
  526.     if (gBlackoutWindow != nil)
  527.         {
  528.         /* Get the initial mouse position in globals */
  529.         GetMouse(&gOldMouse);
  530.         LocalToGlobal(&gOldMouse);
  531.         
  532.         /* Get the initial modifier keys */
  533.         gOldModifiers = gTheEvent.modifiers & (cmdKey + shiftKey + optionKey + controlKey + alphaLock);
  534.                 
  535.         /* Hide the cursor from people */
  536.         HideCursor();
  537.         
  538.         gOldTime = TickCount();
  539.         
  540.         gHaveBlackout = false;
  541.         gContinueRunning = true;
  542.         }
  543.             
  544. } // Initialize
  545.  
  546. /* ------------------------------------------------------------------------------------    */
  547. #pragma segment Main
  548.  
  549. main()
  550. {
  551.     long        timer;
  552.     
  553.     Initialize();
  554.     UnloadSeg((Ptr) Initialize);
  555.     
  556.     if (gBlackoutWindow != nil)
  557.         {
  558.         
  559.         do
  560.             {
  561.             timer = -TickCount();
  562.             WaitNextEvent(everyEvent, &gTheEvent, gSleepTime, nil);
  563.             timer += TickCount();
  564.             
  565.             if (timer > gSleepTime+1)
  566.                 ++gSleepTime;
  567.             else
  568.                 --gSleepTime;
  569.                 
  570.             if (gSleepTime < gMinSleepTime)
  571.                 gSleepTime = gMinSleepTime;
  572.             else
  573.                 if (gSleepTime > gMaxSleepTime)
  574.                     gSleepTime = gMaxSleepTime;
  575.                     
  576.             if (! BlackoutEvent(&gTheEvent))
  577.                 {
  578.                 /* Abort if mouse moved */
  579.                 if ((gTheEvent.where.h != gOldMouse.h) ||
  580.                     (gTheEvent.where.v != gOldMouse.v))
  581.                     gContinueRunning = false;
  582.                     
  583.                     
  584.                 /* Abort if modifiers change */
  585.                 if ((gTheEvent.modifiers & (cmdKey + shiftKey + optionKey + controlKey + alphaLock)) != gOldModifiers)
  586.                     {
  587.                     // clear the cmdKey
  588.                     gOldModifiers &= ~(cmdKey);
  589.                     
  590.                     if ((gTheEvent.modifiers & (cmdKey + shiftKey + optionKey + controlKey + alphaLock)) != gOldModifiers) 
  591.                         gContinueRunning = false;
  592.                     }
  593.                                 
  594.                 switch (gTheEvent.what)
  595.                     {
  596.                     /* Abort if user event */
  597.                     case keyDown:
  598.                     case autoKey:
  599.                     case mouseDown:
  600.                     case mouseUp:
  601.                         gContinueRunning = false;
  602.                         break;
  603.                         
  604.                     case diskEvt:
  605.                         if ( HiWord(gTheEvent.message) != noErr ) 
  606.                             {
  607.                             Point    where;
  608.                         
  609.                             SetPt(&where, 70, 50);
  610.                             ShowCursor();
  611.                             (void) DIBadMount(where, gTheEvent.message);
  612.                             }
  613.                         gContinueRunning = false;
  614.                         
  615.                         break;
  616.                         
  617.                     /* If update event, clean out the update */
  618.                     case updateEvt:
  619.                         {
  620.                         PenState    oldPenState;
  621.                         RGBColor    oldForeColor, oldBackColor;
  622.                         long        oldOldFore, oldOldBack;
  623.                         
  624.                         BeginUpdate((WindowPtr) gTheEvent.message);
  625.                         SetPort((GrafPtr) gTheEvent.message);
  626.                         
  627.                         if  ( (WindowPtr)gTheEvent.message == gBlackoutWindow )
  628.                             {
  629.                             /* redraw menu bar hidden */
  630.                             *mBarHeight = 0;
  631.                             DrawMenuBar();
  632.                             
  633.                             /* add it back into the visRgn of the window */
  634.                             AddMenuBarIntoVisRgn(gBlackoutWindow, gOldMenuBarHeight);
  635.                             
  636.                             /* redraw the menu bar un-hidden */
  637.                             *mBarHeight = gOldMenuBarHeight;
  638.                             DrawMenuBar();
  639.                             }
  640.                         
  641.                         /* Save QuickDraw state and colors */
  642.                         GetPenState(&oldPenState);
  643.                         if (theEnvirons.hasColorQD)
  644.                             {
  645.                             GetForeColor(&oldForeColor);
  646.                             GetBackColor(&oldBackColor);
  647.                             }
  648.                         else
  649.                             {
  650.                             oldOldFore = gBlackoutWindow->fgColor;
  651.                             oldOldBack = gBlackoutWindow->bkColor;
  652.                             }
  653.                             
  654.                         /* Set up and erase */
  655.                         PenNormal();
  656.                         ForeColor(blackColor);
  657.                         BackColor(whiteColor);
  658.                         FillRect( &((GrafPtr)gTheEvent.message)->portRect , qd.black);
  659.                         
  660.                         /* restore QuickDraw state and colors */
  661.                         SetPenState(&oldPenState);
  662.                         if (theEnvirons.hasColorQD)
  663.                             {
  664.                             RGBForeColor(&oldForeColor);
  665.                             RGBBackColor(&oldBackColor);
  666.                             }
  667.                         else
  668.                             {
  669.                             ForeColor(oldOldFore);
  670.                             BackColor(oldOldBack);
  671.                             }
  672.                             
  673.                         EndUpdate((WindowPtr) gTheEvent.message);
  674.                         }
  675.                         break;
  676.                         
  677.                     /* Activate means window has come to front */
  678.                     case activateEvt:
  679.                         if (!gHaveBlackout)
  680.                             {
  681.                             /* Make sure Menu bar is part of the window */
  682.                             AddMenuBarIntoVisRgn(gBlackoutWindow, gOldMenuBarHeight);
  683.                             
  684.                             gHaveBlackout = true;
  685.                             
  686.                             /* Start up the screen saver */
  687.                             gContinueRunning = InitBlackout(gTheBlackout, gBlackoutWindow);
  688.                                                         
  689.                             /* Now window is updated */
  690.                             ValidRect(&gBlackoutWindow->portRect);
  691.                             
  692.                             /* Get the initial mouse position in globals in case the init causes a
  693.                                 move of the mouse for the OK button of a dialog or something. */
  694.                             GetMouse(&gOldMouse);
  695.                             LocalToGlobal(&gOldMouse);
  696.                             
  697.                             /* Make the mouse invisible until moved */
  698.                             ObscureCursor();
  699.  
  700.                             /* restore previous menu bar height, for notification manager */
  701.                             *mBarHeight = gOldMenuBarHeight;
  702.                             }
  703.                         break;
  704.                         
  705.                     /* If a suspend event, Abort */
  706.                     case kOSEvent:
  707.                         switch (gTheEvent.message >> 24) {        /* high byte of message */
  708.                             case kSuspendResumeMessage:
  709.                                 if ((gTheEvent.message & kResumeMask) == 0)
  710.                                     gContinueRunning = false;
  711.                                 break;
  712.                         }
  713.                         break;
  714.                         
  715.                     default:
  716.                         break;
  717.                         
  718.                     } // switch(gTheEvent.what)
  719.                     
  720.                 } // ! BlackoutEvent
  721.                 
  722.             
  723.             if ( (gHaveBlackout) && (gContinueRunning) )
  724.                 {
  725.                 /* Give the Blackout some time */
  726.                 BlackoutIdle(gBlackoutWindow, gTheBlackout);
  727.                 
  728.                 /* Make sure the cursor is hidden every 10 seconds, if visible (MPW is fond of doing this to us) */
  729.                 if (gOldTime <= gTheEvent.when)
  730.                     {
  731.                     ObscureCursor();
  732.                     gOldTime = TickCount() + 600;
  733.                     }
  734.                 }
  735.  
  736.             UnloadSeg((Ptr) Initialize);
  737.                                 
  738.             } while (gContinueRunning);
  739.         
  740.         if (gHaveBlackout)
  741.             {
  742.             /* Close down the screen saver */
  743.             DisposeBlackout(gTheBlackout);
  744.             }
  745.             
  746.         /* Get rid of our window */
  747.         DisposeWindow(gBlackoutWindow);
  748.  
  749.         /* Show the cursor to the nice people */
  750.         ShowCursor();
  751.         } 
  752.         
  753.     /* Restore previous menu bar height */
  754.     DrawMenuBar();
  755.  
  756.     /* Turn on AppleShare dialogs - not currently supported in the AFP driver. */
  757.     //AppleShareDialogs(true);
  758.     
  759.     /* Make sure color table is updated */
  760.     PaintBehind(nil, GetGrayRgn());
  761.     
  762. } // main